/*
 * TOP SECRET
 * Copyright 2006-2015 Transsion.com All right reserved. This software is the
 * confidential and proprietary information of Transsion.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Transsion.com.
 */
package com.palmplaystore.bigdata.storm.appupdate;

import java.util.List;
import java.util.Map;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

import com.palmplaystore.bigdata.ContextHolder;
import com.palmplaystore.bigdata.storm.appupdate.bean.AppObject;


/** 
 * ClassName:AppSaveBolt <br/> 
 * Date:     2018年9月3日 下午5:55:23 <br/> 
 * @author   fenglibin 
 * @version  
 * @see       
 */
public class AppSaveBolt extends BaseRichBolt {

    private static final long serialVersionUID = -2593207785455860511L;
    
    OutputCollector collector;
    
    AppHBaseService appHBaseService;

    @Override
    public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
        appHBaseService = ContextHolder.getApplicationContext().getBean(AppHBaseService.class);
    }

    @Override
    public void execute(Tuple input) {
//        AppObject appObject = (AppObject)input.getValueByField("app");
//        appHBaseService.save(appObject);
    	List<AppObject> appObjects = (List<AppObject>)input.getValueByField("app");
        if(appObjects!=null&&appObjects.size()>0) {
    		AppObject all=appObjects.get(appObjects.size()-1);
    		appObjects.remove(appObjects.size()-1);
    		boolean flag=appHBaseService.saveBatch(appObjects,all);
    		if(!flag) {
    			collector.fail(input);
    			return ;
    		}
    	}
        collector.ack(input);
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {

    }

}
